const cloud = require('wx-server-sdk')
cloud.init({
    env: process.env.ENV_ID
})

exports.main = async (event, context) => {
    const { fileId } = event
    const { data: fileContent } = await cloud.downloadFile({
        fileID: fileId
    })

    const templates = JSON.parse(fileContent.toString())
    const db = cloud.database()

    const batchSize = 100
    for (let i = 0; i < templates.length; i += batchSize) {
        const batch = templates.slice(i, i + batchSize).map(item => ({
            ...item,
            templateUrl: item.template,
            previewImageUrl: item.imgPreview
        }))

        await db.collection('resume_templates').add({
            data: batch
        })
    }

    return {
        success: true,
        importedCount: templates.length
    }
}